package com.pospi.xstore;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.lany.sp.SPHelper;
import com.pospi.xstore.dto.CashierMsgDto;
import com.pospi.xstore.greendao.DaoMaster;
import com.pospi.xstore.greendao.DaoSession;
import com.tsy.sdk.myokhttp.MyOkHttp;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

public class App extends Application {
    private static Context context;
    private static List<Activity> activities = new ArrayList<>();
    private static CashierMsgDto cashierMsgDto;
    private MyOkHttp myOkHttp;
    public static boolean IsPrinter;//是否使用打印机呢
    private static App app;

    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;

    public static List<Activity> getActivities() {
        return activities;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        initDataBase();
        //初始化网络请求
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(15000L, TimeUnit.MILLISECONDS)
                .readTimeout(15000L, TimeUnit.MILLISECONDS).build();
        myOkHttp = new MyOkHttp(okHttpClient);
        //初始化sharedprefece
        SPHelper.getInstance().init(this, com.lany.sp.BuildConfig.DEBUG);


    }
    public MyOkHttp getMyOkHttp() {
        return myOkHttp;
    }

    public static void killAllActivities() {
        for (Activity activity : activities) {
            activity.finish();
        }
    }
    public DaoSession getDaoSession() {
        return daoSession;
    }
    public static App getInstance(){
        return app;
    }
    //初始化greendao
    private void initDataBase() {
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this, "pospi_db", null);
        db = openHelper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }
    public static Context getContext() {
        return context;
    }
    public static CashierMsgDto getCashierMsgDto() {
        return cashierMsgDto;
    }

    public static void setCashierMsgDto(CashierMsgDto cashierMsgDto) {
        App.cashierMsgDto = cashierMsgDto;
    }
}
